﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using T.FAS.Runtime.UserProfile.Dao;
using T.FAS.Runtime.UserProfile.Entity;

namespace T.FAS.Runtime.UserProfile.Service
{
    /// <summary>
    /// 组件元数据服务
    /// </summary>
    public class UserProfilesManager
    {
        UserProfileGroupDao userProfileGroupDao;
        UserProfileItemDao userProfileItemDao;
        UserProfilesDao userProfilesDao;

        /// <summary>
        /// 获取所有配置项分组
        /// </summary>
        public List<UserProfileGroup> GetUserProfileGroups()
        {
            if (userProfileGroupDao == null)
            {
                userProfileGroupDao = new UserProfileGroupDao();
            }
            if (userProfileItemDao == null)
            {
                userProfileItemDao = new UserProfileItemDao();
            }
            List<UserProfileGroup> userProfileGroups = userProfileGroupDao.GetUserProfileGroups();
            return userProfileGroups;
        }
        /// <summary>
        /// 新增或修改
        /// </summary>
        public Dictionary<string, string> CreateOrUpdateUserProfile(UserProfiles profile)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                if (userProfilesDao == null)
                {
                    userProfilesDao = new UserProfilesDao();
                }
               string UserName =  UserUtil.GetCurrentUserName();
                profile.LastModifier = UserName;
                profile.LastModifyTime = DateTime.Now;
                if (string.IsNullOrEmpty(profile.ID))
                {
                    profile.ID = Guid.NewGuid().ToString();
                    profile.UserID = UserUtil.GetCurrentUserID();
                    profile.Creator = UserName;
                    profile.CreateTime = DateTime.Now;
                    userProfilesDao.Add(new List<object> { profile });
                }
                else
                {
                    profile.UserID = UserUtil.GetCurrentUserID();
                    profile.Creator = UserName;
                    profile.CreateTime = DateTime.Now;
                    userProfilesDao.Update(new List<object> { profile });
                }
                dic.Add("state", "1");
                dic.Add("message", "保存成功");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        public Dictionary<string, string> DeleteUserProfile(string profileID)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            try
            {
                if (userProfilesDao == null)
                {
                    userProfilesDao = new UserProfilesDao();
                }
                userProfilesDao.DeleteUserProfiles(profileID);
                dic.Add("state", "1");
                dic.Add("message", "删除成功");
            }
            catch (Exception e)
            {
                dic.Add("state", "0");
                dic.Add("errmsg", e.ToString());
            }
            return dic;
        }
        /// <summary>
        /// 获取用户配置列表
        /// </summary>
        public UserProfiles GetUserProfile(string profileItemID)
        {
            if (userProfilesDao == null)
            {
                userProfilesDao = new UserProfilesDao();
            }
            UserProfiles userProfiles = userProfilesDao.GetUserProfile(profileItemID);
            if (userProfiles == null)
            {
                if (userProfileItemDao == null)
                {
                    userProfileItemDao = new UserProfileItemDao();
                }
                UserProfileItem userProfileItem = userProfileItemDao.GetObject(profileItemID);
                if (userProfileItem != null)
                {
                    userProfiles = new UserProfiles
                    {
                        ItemID = profileItemID,
                        ProfileValue = userProfileItem.DefaultValue,
                        ProfileText = userProfileItem.DefaultText
                    };
                    userProfiles.ItemCode = userProfileItem.Code;
                    userProfiles.ItemName = userProfileItem.Name;
                    userProfiles.ItemDescription = userProfileItem.Description;
                    userProfiles.ItemHelpID = userProfileItem.HelpID;
                }
            }
            return userProfiles;
        }
        /// <summary>
        /// 获取用户配置列表
        /// </summary>
        public List<UserProfiles> GetUserProfiles(string profileGroupID)
        {
            if (userProfilesDao == null)
            {
                userProfilesDao = new UserProfilesDao();
            }
            if (userProfileItemDao == null)
            {
                userProfileItemDao = new UserProfileItemDao();
            }
            List<UserProfiles> userProfiles = new List<UserProfiles>();
            List<UserProfileItem> userProfileItems = userProfileItemDao.GetUserProfileItems(profileGroupID);
            List<UserProfiles> userProfileList = userProfilesDao.GetUserProfiles(profileGroupID);
            foreach (var item in userProfileItems)
            {
                UserProfiles profiles = userProfileList.FirstOrDefault(t=>t.ItemID == item.ID);
                if (profiles == null)
                {
                    if (userProfileItemDao == null)
                    {
                        userProfileItemDao = new UserProfileItemDao();
                    }
                    profiles = new UserProfiles
                    {
                        ItemID = item.ID,
                        ProfileValue = item.DefaultValue,
                        ProfileText = item.DefaultText
                    };
                }
                profiles.ItemCode = item.Code;
                profiles.ItemName = item.Name;
                profiles.ItemDescription = item.Description;
                profiles.ItemHelpID = item.HelpID;
                userProfiles.Add(profiles);
            }
            return userProfiles;
        }
    }
}
